/**
 * Copyright 2016-2018 mobaas.com
 */
package com.mobaas.devops.node.docker;

import java.util.HashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mobaas.devops.node.HttpResult;
import com.mobaas.devops.node.HttpUtil;

/**
 * 
 * @author billy zhang
 *
 */
public class DockerClient {

	private static final Logger log = LoggerFactory.getLogger(DockerClient.class);
	
	private String host;
	private DockerCertificate certificate;
	
	public DockerClient(String host, String certPath) throws DockerCertificateException {
		this.host = "https://" + host + ":2376";
		this.certificate = new DockerCertificate(certPath);
	}
	
	// ------ Containers -------------
	public HttpResult listContainers() {
		
		String action = "/containers/json";
		
		return HttpUtil.getSSL(host + action, certificate.sslContext());
		
	}
	
	public HttpResult createContainer(String image, String config) {
		log.info("create container from image:" + image);
		
		String action = "/containers/create";
		
		HttpResult result = HttpUtil.postSSL(host + action, config, certificate.sslContext());
		log.info(result.getInfo());
		
		return result;
	}
	
	public HttpResult startContainer(String id) {
		log.info("start container: " + id);
		
		String action = "/containers/" + id + "/start";
		
		HttpResult result = HttpUtil.postSSL(host + action, null, certificate.sslContext());
		log.info(result.getInfo());
		
		return result;
	}
	
	public HttpResult restartContainer(String id) {
		log.info("restart container: " + id);
		
		String action = "/containers/" + id + "/restart";
		
		HttpResult result = HttpUtil.postSSL(host + action, null, certificate.sslContext());
		log.info(result.getInfo());
		
		return result;
	}

	public HttpResult stopContainer(String id) {
		log.info("stop container: " + id);
		
		String action = "/containers/" + id + "/stop";
		
		HttpResult result = HttpUtil.postSSL(host + action, null, certificate.sslContext());
		log.info(result.getInfo());
		
		return result;
	}
	
	public HttpResult inspectContainer(String id) {

		String action = "/containers/" + id + "/json";
		
		return HttpUtil.getSSL(host + action, certificate.sslContext());
		
	}

	public HttpResult removeContainer(String id) {
		log.info("remove container: " + id);
		
		String action = "/containers/" + id;
		
		HttpResult result = HttpUtil.deleteSSL(host + action, certificate.sslContext());
		log.info(result.getInfo());
		
		return result;
	}
	
	public HttpResult getLogs(String id) {
		log.info("get container logs: " + id);
		
		String action = String.format("/containers/%s/logs?stdout=true", id);
		
		HttpResult result = HttpUtil.getSSL(host + action, certificate.sslContext());
		log.info(result.getInfo());
		
		return result;
	}
	
	public HttpResult getContainerStats(String containerId) {
		String action = String.format("/containers/%s/stats?stream=false", containerId);
		
		return HttpUtil.getSSL(host + action, certificate.sslContext());
	}
	
	//------ Image ----------
	public HttpResult inspectImage(String name, String tag) {

		String action = String.format("/containers/%s:%s/json", name, tag);
		
		return HttpUtil.getSSL(host + action, certificate.sslContext());
	}
	
	public HttpResult buildImage(byte[] tarData, String name, RegistryAuth auth) {

		log.info("build image: " + name);
		
		String action = String.format("/build?t=%s", name);
		
		//header: "X-Registry-Config"
		HashMap<String, String> headers = new HashMap<>();
		headers.put("X-Registry-Config", auth.toJsonForConfig());
		log.info("X-Registry-Config: " + auth.toString());
		
		HttpResult result = HttpUtil.postSSL(host + action, tarData, "application/x-tar", headers, certificate.sslContext());
		log.info(result.getInfo());
		
		return result;
	}
	
	public HttpResult pushImage(String name, String tag, RegistryAuth auth) {

		log.info("push image: " + name + ":" + tag);
		
		String action = String.format("/images/%s/push?tag=%s", name, tag);
		
		//header: "X-Registry-Auth"
		HashMap<String, String> headers = new HashMap<>();
		headers.put("X-Registry-Auth", auth.toJsonForAuth());
		log.info("X-Registry-Auth: " + auth.toString());
		
		HttpResult result = HttpUtil.postSSL(host + action, "{}", headers, certificate.sslContext());
		log.info(result.getInfo());
		
		return result;
	}

	public HttpResult pullImage(String name, String tag, RegistryAuth auth) {
		log.info("pull image: " + name + ":" + tag);
		
		String action = String.format("/images/create?fromImage=%s&tag=%s", name, tag);
		
		//header: "X-Registry-Auth"
		HashMap<String, String> headers = new HashMap<>();
		headers.put("X-Registry-Auth", auth.toJsonForAuth());
		log.info("X-Registry-Auth: " + auth.toString());
		
		HttpResult result = HttpUtil.postSSL(host + action, "{}", headers, certificate.sslContext());
		log.info(result.getInfo());
		
		return result;
	}
	
}
